package com.tour.controller.admin;

import com.tour.controller.LoginModel;

import com.tour.dao.*;

import com.tour.model.*;

import com.tour.service.*;
import com.tour.service.impl.*;

import com.tour.util.*;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/admin/gl_info")
public class AGlInfoController {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    GlInfoService glInfoService;
    @Autowired
    TypeInfoMapper typeInfoMapper;
    @Autowired
    GlInfoMapper glInfoMapper;
    @Autowired
    AdminInfoMapper adminInfoMapper;

    public void getList(ModelMap modelMap, LoginModel login) { //获取数据列表并返回给前台

        TypeInfoExample typeInfoE = new TypeInfoExample();
        TypeInfoExample.Criteria typeInfoC = typeInfoE.createCriteria();
        List<TypeInfo> typeInfoList = typeInfoMapper.selectByExample(typeInfoE);
        List<Map<String, Object>> typeInfoList2 = new ArrayList<Map<String, Object>>();

        for (TypeInfo m : typeInfoList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", m.getId());
            map.put("name", m.getTypeName());
            typeInfoList2.add(map);
        }

        modelMap.addAttribute("typeInfoList", typeInfoList2);
        modelMap.addAttribute("glLevelList", DataListUtils.getGlLevelList()); //返回gl_level数据列表
        modelMap.addAttribute("checkStatusList",
            DataListUtils.getCheckStatusList()); //返回check_status数据列表
    }

    /**
     * 返回攻略列表jsp页面
    */
    @RequestMapping(value = "")
    public String index(ModelMap modelMap, HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName); //获取当前登录账号信息
        AdminInfo adminInfo = adminInfoMapper.selectByPrimaryKey(login.getId());
        modelMap.addAttribute("user", adminInfo);
        getList(modelMap, login); //获取数据列表并返回给前台

        return "admin/gl_info/list";
    }

    /**
     * 根据查询条件分页查询攻略数据,然后返回给前台渲染
    */
    @RequestMapping(value = "queryList")
    @ResponseBody
    public Object toList(GlInfo model, Integer page, HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);

        return glInfoService.getDataList(model, page, CommonVal.pageSize, login); //分页查询数据
    }

    /**
     查看详情接口
    */
    @RequestMapping("detail1")
    public Object detail1(Integer id, ModelMap modelMap,
        HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);
        GlInfo model = new GlInfo();
        model.setId(id);

        Map<String, Object> rs = glInfoService.getDataList(model, null, null,
                login);
        List<Map<String, Object>> tmplist = (List<Map<String, Object>>) rs.get(
                "list");

        if ((tmplist != null) && (tmplist.size() > 0)) {
            modelMap.addAttribute("detail", tmplist.get(0));
        } else {
            modelMap.addAttribute("detail", new HashMap<String, Object>());
        }

        return "admin/gl_info/detail";
    }

    /**
     删除数据接口
    */
    @RequestMapping("del")
    @ResponseBody
    public Object del(Integer id, ModelMap modelMap, HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);
        Map<String, Object> rs = new HashMap<String, Object>();
        glInfoService.delete(id);
        rs.put("code", 1);
        rs.put("msg",
            "删除成功");

        return rs;
    }

    /**
     进入审核通过页面
    */
    @RequestMapping("tg")
    public String tg(ModelMap modelMap, GlInfo model, HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName); //从session中获取当前登录账号	
        getList(modelMap, login); //获取前台需要用到的数据列表并返回给前台

        GlInfo data = glInfoMapper.selectByPrimaryKey(model.getId());

        if (data.getContent() != null) {
            data.setContent(data.getContent().replaceAll("'", "\\\\'")); //wangeditor需要过滤掉'符号,否则初始化可能报错
        }

        modelMap.addAttribute("data", data);

        return "admin/gl_info/tg_page";
    }

    /**
     审核通过提交信息接口
    */
    @RequestMapping("tg_submit")
    @ResponseBody
    public Object tg_submit(GlInfo model, ModelMap modelMap,
        HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);
        Map<String, Object> rs = new HashMap<String, Object>();
        String msg = glInfoService.tg(model, login); //执行修改操作

        if (msg.equals("")) {
            rs.put("code", 1);
            rs.put("msg",
                "修改成功");

            return rs;
        }

        rs.put("code", 0);
        rs.put("msg", msg);

        return rs;
    }

    /**
     进入审核不通过页面
    */
    @RequestMapping("btg")
    public String btg(ModelMap modelMap, GlInfo model,
        HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName); //从session中获取当前登录账号	
        getList(modelMap, login); //获取前台需要用到的数据列表并返回给前台

        GlInfo data = glInfoMapper.selectByPrimaryKey(model.getId());

        if (data.getContent() != null) {
            data.setContent(data.getContent().replaceAll("'", "\\\\'")); //wangeditor需要过滤掉'符号,否则初始化可能报错
        }

        modelMap.addAttribute("data", data);

        return "admin/gl_info/btg_page";
    }

    /**
     审核不通过提交信息接口
    */
    @RequestMapping("btg_submit")
    @ResponseBody
    public Object btg_submit(GlInfo model, ModelMap modelMap,
        HttpServletRequest request) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute(CommonVal.sessionName);
        Map<String, Object> rs = new HashMap<String, Object>();
        String msg = glInfoService.btg(model, login); //执行修改操作

        if (msg.equals("")) {
            rs.put("code", 1);
            rs.put("msg",
                "修改成功");

            return rs;
        }

        rs.put("code", 0);
        rs.put("msg", msg);

        return rs;
    }
}

